#include "CLogDatabaseMgr.h"

void CLogDatabaseMgr::AddLogDatabase(const std::string& dbName, CLogDatabasePtr logDatabase)
{
	if (logDatabase && !dbName.empty())
	{
		m_logDatabaseMap[dbName] = logDatabase;
	}
}

CLogDatabasePtr CLogDatabaseMgr::GetLogDatabase(const std::string& dbName) const
{
	auto it = m_logDatabaseMap.find(dbName);
	if (it != m_logDatabaseMap.end())
	{
		return it->second;
	}
	return nullptr;
}

bool CLogDatabaseMgr::HasLogDatabase(const std::string& dbName) const
{
	return m_logDatabaseMap.find(dbName) != m_logDatabaseMap.end();
}

void CLogDatabaseMgr::RemoveLogDatabase(const std::string& dbName)
{
	auto it = m_logDatabaseMap.find(dbName);
	if (it != m_logDatabaseMap.end())
	{
		m_logDatabaseMap.erase(it);
	}
}

int CLogDatabaseMgr::InitAllDatabases()
{
	for (auto& pair : m_logDatabaseMap)
	{
		CLogDatabasePtr db = pair.second;
		if (db)
		{
			int ret = db->initDatabase();
			if (ret != 0)
			{
				return ret; // Return error code if initialization fails
			}
		}
	}
	return 0; // All databases initialized successfully
}

CLogDatabaseMgr::~CLogDatabaseMgr()
{
	// Clear the log database map
	m_logDatabaseMap.clear();
}
